package Frontend

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
)

func Get_profession_course(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	type Children struct {
		Value int `db:"id"json:"value"`
		Label string `db:"name"json:"label"`
	}
	type Children_class struct {
		Value int `db:"id"json:"value"`
		Label string `db:"name"json:"label"`
		Childrens []Children `db:"children"json:"children"`
	}
	sqlStr := "select id, name from profession;"
	var users []Children_class
	err := db.Select(&users, sqlStr)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	//fmt.Printf("users:%#v\n", users)
	for i:=0;i<len(users);i++ {
		sqlStr := "select id, name from course where profession_id=?;"
		var users2 []Children
		err := db.Select(&users2, sqlStr,users[i].Value)
		if err != nil {
			fmt.Printf("query failed, err:%v\n", err)
			return
		}
		for x:=0;x<len(users2);x++ {
			users[i].Childrens = append(users[i].Childrens, users2[x])
		}
	}
	data, err := json.Marshal(users)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	fmt.Printf("json:%s\n", data)
	w.Write([]byte(data)) //给客户端返回数据。
}
// 修改班级资料
func Admin_get_class(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	type class_arr struct {
		Id int `db:"id"json:"id"`
		Name string `db:"name"json:"name"`
		Persons_max int `db:"persons_max"json:"persons_max"`
		Persons_curr int `db:"persons_curr"json:"persons_curr"`
		Money float32 `db:"money"json:"money"`
		Tu_url string `db:"tu_url"json:"tu_url"`
		Course_id int `db:"course_id"json:"course_id"`
		Course_name string `db:"course_name"json:"course_name"`
		Profession_id int `db:"profession_id"json:"profession_id"`
		Profession_name string `db:"profession_name"json:"profession_name"`
	}
	sqlStr := `SELECT c.id AS id,c.name AS name,c.persons_max AS persons_max,c.persons_curr AS persons_curr,c.money AS money,c.tu_url AS tu_url,cc.id AS course_id,cc.name AS course_name,p.id AS profession_id,p.name AS profession_name FROM class c LEFT JOIN course cc ON c.course_id=cc.id LEFT JOIN profession p ON cc.profession_id=p.id;`
	var users []class_arr
	err := db.Select(&users, sqlStr)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	//fmt.Printf("users:%#v\n", users)
	data, err := json.Marshal(users)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	//fmt.Printf("json:%s\n", data)
	w.Write([]byte(data)) //给客户端返回数据。
}
// 更新用户信息
func Admin_up_class(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	id_int,_:=strconv.Atoi(r.PostFormValue("id"))
	course_id_int,_:=strconv.Atoi(r.PostFormValue("course_id"))
	course_max_int,_:=strconv.Atoi(r.PostFormValue("persons_max"))
	course_max_curr,_:=strconv.Atoi(r.PostFormValue("persons_curr"))
	money_float,_:= strconv.ParseFloat(r.PostFormValue("money"), 32)
	my_class:=mysql_class{
		Id:           id_int,
		Name:         r.PostFormValue("name"),
		Course_id:    course_id_int,
		Persons_max:  course_max_int,
		Persons_curr: course_max_curr,
		Money:        float32(money_float),
		Tu_url:       r.PostFormValue("tu_url"),
		Choice:       "0",
		Isok:         false,
	}
	sqlStr := `update class set name=?,course_id=?,persons_max=?,persons_curr=?,money=?,tu_url=? where id =?;`
	ret, err := db.Exec(sqlStr, my_class.Name,my_class.Course_id,my_class.Persons_max,my_class.Persons_curr,my_class.Money,my_class.Tu_url,my_class.Id)
	if err != nil {
		fmt.Printf("update failed, err:%v\n", err)
		return
	}
	n, err := ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		jsonk := `{"isok":"no","msg":"修改失败"}`
		w.Write([]byte(jsonk)) //给客户端返回数据。
		return
	}
	fmt.Printf("update success, affected rows:%d\n", n)
	jsonk := `{"isok":"ok","msg":"修改成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
	return
}

// 写入用户信息
func Admin_set_class(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	id_int,_:=strconv.Atoi(r.PostFormValue("id"))
	course_id_int,_:=strconv.Atoi(r.PostFormValue("course_id"))
	course_max_int,_:=strconv.Atoi(r.PostFormValue("persons_max"))
	course_max_curr,_:=strconv.Atoi(r.PostFormValue("persons_curr"))
	money_float,_:= strconv.ParseFloat(r.PostFormValue("money"), 32)
	my_class:=mysql_class{
		Id:           id_int,
		Name:         r.PostFormValue("name"),
		Course_id:    course_id_int,
		Persons_max:  course_max_int,
		Persons_curr: course_max_curr,
		Money:        float32(money_float),
		Tu_url:       r.PostFormValue("tu_url"),
		Choice:       "0",
		Isok:         false,
	}
	sqlStr := "insert into class(name,course_id,persons_max,persons_curr,money,tu_url,choice) values (?,?,?,?,?,?,?)"
	ret, err := db.Exec(sqlStr, my_class.Name,my_class.Course_id,my_class.Persons_max,my_class.Persons_curr,my_class.Money,my_class.Tu_url,"0")
	if err != nil {
		fmt.Printf("insert failed, err:%v\n", err)
		return
	}
	theID, err := ret.LastInsertId() // 新插入数据的id
	if err != nil {
		jsonk := `{"isok":"no","msg":"插入失败"}`
		w.Write([]byte(jsonk)) //给客户端返回数据。
		return
	}
	fmt.Printf("insert success, the id is %d.\n", theID)
	jsonk := `{"isok":"ok","msg":"插入成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
	return
}

// 删除班级 随便删除所有该班级的记录
func Admin_dele_class(w http.ResponseWriter, r *http.Request)  {

	w.Header().Set("Access-Control-Allow-Origin", "*")
	// 删除班级
	id_int,_:=strconv.Atoi(r.PostFormValue("id"))
	sqlStr := "delete from class where id = ?"
	ret, err := db.Exec(sqlStr, id_int)
	if err != nil {
		fmt.Printf("delete failed, err:%v\n", err)
		return
	}
	n, err := ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		return
	}
	fmt.Printf("delete success, affected rows:%d\n", n)

	// 删除报名记录
	sqlStr = "delete from register_record where class_id = ?"
	ret, err = db.Exec(sqlStr, id_int)
	if err != nil {
		fmt.Printf("delete failed, err:%v\n", err)
		return
	}
	n, err = ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		return
	}
	fmt.Printf("delete success, affected rows:%d\n", n)
	jsonk := `{"isok":"ok","msg":"删除成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
	return
}